#include <stdio.h>
#include <stdbool.h>

// 1. 打印三角形
void printTriangle() {
    int rows = 6;
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= rows - i; j++) printf(" ");
        for (int k = 1; k <= 2 * i - 1; k++) printf("*");
        printf("\n");
    }
}

// 2. 打印九九乘法表
void printMultiplicationTable() {
    for (int i = 1; i <= 9; i++) {
        for (int j = 1; j <= i; j++)
            printf("%d*%d=%-2d ", i, j, i * j);
        printf("\n");
    }
}

// 3. 打印杨辉三角
void printYangHuiTriangle() {
    int n = 6, triangle[6][6] = {0};
    
    // 生成杨辉三角
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i) triangle[i][j] = 1;
            else triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
        }
    }
    
    // 打印杨辉三角
    for (int i = 0; i < n; i++) {
        for (int k = 0; k < n - i - 1; k++) printf(" ");
        for (int j = 0; j <= i; j++) printf("%d ", triangle[i][j]);
        printf("\n");
    }
}

// 4. 鸡兔同笼问题
void solveChickenRabbit() {
    int heads, legs;
    printf("请输入总头数和总脚数：");
    scanf("%d %d", &heads, &legs);

    if (legs % 2 != 0 || heads <= 0 || legs <= 0) {
        printf("输入无效\n");
        return;
    }

    int rabbits = (legs - 2 * heads) / 2;
    int chickens = heads - rabbits;

    if (rabbits >= 0 && chickens >= 0)
        printf("鸡有 %d 只，兔有 %d 只\n", chickens, rabbits);
    else
        printf("无解\n");
}

// 5. 求100以内质数之和
void sumPrimes() {
    int sum = 0;
    for (int num = 2; num <= 100; num++) {
        bool is_prime = true;
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                is_prime = false;
                break;
            }
        }
        if (is_prime) sum += num;
    }
    printf("100以内的所有质数之和为：%d\n", sum);
}

int main() {
    int choice;
    do {
        printf("\n===== 功能菜单 =====\n");
        printf("1. 打印三角形\n");
        printf("2. 打印九九乘法表\n");
        printf("3. 打印杨辉三角\n");
        printf("4. 解决鸡兔同笼问题\n");
        printf("5. 计算100以内质数之和\n");
        printf("0. 退出程序\n");
        printf("请输入你的选择：");
        scanf("%d", &choice);

        switch (choice) {
            case 1: printTriangle(); break;
            case 2: printMultiplicationTable(); break;
            case 3: printYangHuiTriangle(); break;
            case 4: solveChickenRabbit(); break;
            case 5: sumPrimes(); break;
            case 0: printf("程序已退出\n"); break;
            default: printf("无效选择，请重新输入！\n");
        }
    } while (choice != 0);

    return 0;
}